﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmNgonNgu : DevExpress.XtraEditors.XtraForm
    {
        #region Khai báo biến
        public string maNgonNgu = "";
        DataTable dt;
        TextMessageBox box = new TextMessageBox();
        NgonNgu_BLL nn_bll = new NgonNgu_BLL();
        private bool isOK = false;
        #endregion

        #region LoadForm

        public frmNgonNgu()
        {
            InitializeComponent();
        }

        private void frmNgonNgu_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion

        #region Các chức năng
        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = false;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        #endregion

        #region Hàm viết thêm
        private void UpdateData()
        {
            if (checkValid())
            {
                NgonNgu_Public nn_public = new NgonNgu_Public();
                if (maNgonNgu == "")
                {
                    if (checkTenInsert())
                    {
                        nn_public.MaNgonNgu = box.AutoID("NN", 2, nn_bll.LayDuLieu());
                        nn_public.TenNgonNgu =box.toUpper(txtTenNN.Text);
                        nn_public.GhiChu = txtGhiChu.Text;

                        nn_bll.Insert(nn_public, nn_public, nn_public);
                        CloseForm();
                    }
                }
                else
                {
                    nn_public.MaNgonNgu = txtMaNgonNgu.Text.Trim();
                    nn_public.TenNgonNgu =box.toUpper(txtTenNN.Text);
                    nn_public.GhiChu = txtGhiChu.Text;

                    nn_bll.Update(nn_public, nn_public, nn_public);
                    CloseForm();
                }
            }
        }
        private void CloseForm()
        {
            if (isOK == true)
                DialogResult=DialogResult.OK;
            else
                SetInterface();
        }
        private void LoadData()
        {
            if (maNgonNgu != "")
            {
                dt = nn_bll.TaoBang(box.sqlCheckExist("DM_NgonNgu","MaNgonNgu",maNgonNgu));
                DataRow dr=dt.Rows[0];
                txtMaNgonNgu.Text = maNgonNgu;
                txtTenNN.Text=dr["TenNgonNgu"].ToString();
                txtGhiChu.Text = dr["GhiChu"].ToString();
            }
        }
        private void SetInterface()
        {
            
            txtMaNgonNgu.Text = "Mã tự sinh";
            txtGhiChu.Text = "";
             maNgonNgu = "";
             txtTenNN.Text = "";
            txtTenNN.Focus();
        }
        private bool checkValid()
        {

            if (txtTenNN.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên ngôn ngữ.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTenNN.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenInsert()
        {
            dt = nn_bll.TaoBang(box.sqlCheckExist("DM_NgonNgu","TenNgonNgu",txtTenNN.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtTenNN.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtTenNN.Focus();
                return false;
            }
            return true;
        }

        #endregion

        #region các sự kiện hệ thống
        private void frmNgonNgu_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }

        
        #endregion
    }
}